<?php

use App\Article;
use App\Tag;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class ArticleTableSeeder extends Seeder
{
    public function run()
    {
        $tags = Tag::lists('tag')->all();
        Article::truncate();

        DB::table('article_tag_pivot')->truncate();
        factory(Article::class, 20)->create()->each(function ($article) use ($tags) {
            if (mt_rand(1, 100) <= 30) {
                return;
            }
            shuffle($tags);
            $articleTags = [$tags[0]];
            if (mt_rand(1, 100) <= 30) {
                $articleTags[] = $tags[1];
            }
            $article->syncTags($articleTags);
        });
    }
}